home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 376_04 / os2tool.003 / WIPEFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-20  |  3.5 KB  |  154 lines

  1. /*
  2. * WIPEFILE.C - Destroys the contents of a file by writing over it
  3. *           multiple times.
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        1.12.1990
  7. */
  8. static char *VERSION ="Version 1.1. Copyright (c) Martti Ylikoski, 1990, 1991." ;
  9. /*
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <param.h>
  17. #include <paramstd.h>
  18.  
  19. static char *progname, *fname    ;
  20. extern unsigned long pflags ;
  21.  
  22. ParamEntry pentry[12] = {
  23.      "P", &ParamSetPause, 0,
  24.      "F", &ParamSetFold, 0,
  25.      "V", &ParamSetVerbose, 1,
  26.      "R", &ParamSetReport, 0,
  27.      "S", &ParamSetSubDirs, 0,
  28.      "?", &ParamSetHelp, 1,
  29.      "H", &ParamSetHelp, 1,
  30.      "NOD", &ParamSetNoDefault, 0,
  31.      "TEST", &ParamSetTest, 1,
  32.      "Y", &ParamSetYes, 1,
  33.      "N", &ParamSetTest, 1,
  34.      "\0", NULL, 0
  35. } ;
  36.  
  37. ParamBlock params = {
  38.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  39.     pentry
  40. } ;
  41.  
  42. static int fhnd, writecnt = 1 ;
  43.  
  44. /* Macro to get a random integer within a specified range */
  45. #define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
  46.  
  47. /* local prototypes */
  48. static int wipefile (char *fname) ;
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52. int i ;
  53.  
  54.    progname = argv[0] ;
  55.  
  56.    ParamHandle(¶ms, &argc, argv) ;
  57.  
  58.    if (pflags & PA_HELP || argc == 1)
  59.    {
  60.       printf("%s - destroy file by writing over it randomized data multiple times.\n", progname) ;
  61.       puts(VERSION) ;
  62.       puts("Usage: wipefile [ -count files | /H | /? | /TEST | /N] ") ;
  63.       return( 1 ) ;
  64.    }
  65.  
  66.    /* scan for overwrite count */
  67.    for (i = 1 ; i < argc; i++)
  68.    {
  69.       if ( (argv[i][0] == '-' || argv[i][0] == '/') && strlen(argv[i]) > 1 )
  70.      writecnt = atoi(&argv[i][1]) ;
  71.       else
  72.      fname = argv[i] ;
  73.    }
  74.  
  75.    /* Seed the random number generator with current time. */
  76.    srand( (unsigned)time( NULL ) );
  77.  
  78.    for (i=1 ; i <argc ; i++)
  79.       if (argv[i][0] != '/' && argv[i][0] != '-')
  80.      wipefile(argv[i]) ;
  81.  
  82.  
  83. }
  84. /***********************************
  85. *  wipefile() - destroy the contents of a file by writing over it multiple
  86. *  times random data. This function assumes that the random number generator
  87. *  has been initialized. The deleting is normally confirmed from user.
  88. *
  89. ************************************/
  90.  
  91. static int wipefile (char *fname)
  92. {
  93. FILE *fptr ;
  94. long lsize, li ;
  95. int i, c ;
  96.  
  97.    if (pflags & PA_TEST)
  98.    {
  99.       printf("Would WIPE %s", fname) ;
  100.       return( 0 ) ;
  101.    }
  102.  
  103.    if ( (pflags & PA_YES) == 0)
  104.    {
  105.       printf("WIPE %s (Y/N) ?", fname) ;
  106.  
  107.       c = getchar() ;
  108.       getchar() ; /* read past newline */
  109.       if (c == 'Y'|| c == 'y')
  110.       {
  111.      ;
  112.       }
  113.       else
  114.       {
  115.      fprintf(stderr, "%s will not be wiped\n", fname) ;
  116.      return( 0 ) ;
  117.       }
  118.    }
  119.  
  120.    if (pflags & PA_VERBOSE)
  121.       printf("WIPING %s ...\n", fname) ;
  122.  
  123.    /* open file */
  124.    if ((fptr = fopen(fname, "rb+")) == NULL)
  125.    {
  126.       printf("%s: unable to open file %s for writing...\n", progname, fname) ;
  127.       printf("Exiting...\n") ;
  128.       return( 1 ) ;
  129.    }
  130.  
  131.    /* determine size */
  132.    fseek(fptr, 0L, SEEK_END);
  133.    lsize = ftell (fptr) ;
  134.    rewind(fptr) ;
  135.  
  136.    /* write multiple times over. */
  137.    for (i = 0 ; i < writecnt ; i++)
  138.    {
  139.       /* write file full of random text */
  140.       for (li = 0 ; li < lsize ; li++)
  141.       {
  142.      fputc(getrandom(1,255), fptr) ;
  143.       }
  144.       rewind(fptr) ;
  145.    }
  146.  
  147.    /* close file */
  148.    fclose (fptr ) ;
  149.  
  150.    if (pflags & PA_VERBOSE)
  151.       printf("WIPED!\n") ;
  152.    return( 0 ) ;
  153. }
  154.